package assetservicelogic

import (
	"context"
	"property_service/dao/model"
	"property_service/dao/query"
	"property_service/internal/svc"
	"property_service/property_service"
	"property_service/utils/BeanUtils"
	"property_service/utils/result"

	"github.com/zeromicro/go-zero/core/logx"
)

type DepreciationInfoEditLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewDepreciationInfoEditLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DepreciationInfoEditLogic {
	return &DepreciationInfoEditLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// DepreciationInfoEdit 更新折旧信息表
func (l *DepreciationInfoEditLogic) DepreciationInfoEdit(in *property_service.DepreciationInfo) (*property_service.Response, error) {
	// 获取折旧信息表模型的查询构造器
	m := query.Use(l.svcCtx.Gorm).DepreciationInfo
	// 构建查询条件：根据ID查询
	do := m.WithContext(l.ctx).Where(m.ID.Eq(in.ID))
	// 执行更新操作，仅更新指定字段，不包含ID、TenantID、UpdatedAt、CreatedAt
	if _, err := do.UpdateColumns(&model.DepreciationInfo{
		AssetID:                      in.AssetID,                            // 资产ID
		AssetValue:                   in.AssetValue,                         //资产原值
		StartDate:                    BeanUtils.GetDateFormat(in.StartDate), // 开始折旧日期
		NetValue:                     in.NetValue,                           // 当前净值
		UsefulLife:                   in.UsefulLife,                         // 使用年限（月）
		ExpectedUsefulLife:           in.ExpectedUsefulLife,                 //预计使用年限（月）
		DepreciationRate:             in.DepreciationRate,                   //月折旧率
		DepreciationRateYear:         in.DepreciationRateYear,               //年折旧率
		NetResidueRate:               in.NetResidueRate,                     //净残值率
		DepreciatedMonths:            in.DepreciatedMonths,                  // 已折旧月份
		StatusID:                     in.StatusID,                           // 状态ID
		StatusName:                   in.StatusName,                         //状态名称
		LastDate:                     in.LastDate,                           // 最近折旧日期
		Method:                       in.Method,                             // 折旧方式
		Remark:                       in.Remark,                             // 折旧备注
		UpdatedAtuser:                in.UpdatedAtuser,                      // 更新用户
		NetResidueValue:              in.NetResidueValue,                    //净残值
		AccumulatedDepreciationValue: in.AccumulatedDepreciationValue,       //累计折旧额
		NetWorthValue:                in.NetWorthValue,                      //净值
		NeteValue:                    in.NeteValue,                          //净额
		CurrencyID:                   in.CurrencyId,                         //币种ID(关联币种表)
		CurrencyName:                 in.CurrencyName,                       //币种名称
	}); err != nil {
		// 如果更新失败，返回错误响应
		return result.Err(err).Ed()
	}
	// 更新成功，返回成功响应
	return result.Suc().EditEd()
}
